package com.litesuits.sdk.views;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.litesuits.android.view.TipsView;
import com.litesuits.sdk.R;

/**
 * @author MaTianyu @http://litesuits.com
 * @date 2015-07-30
 */
public class DefaultTipsView extends TipsView {
    protected ViewGroup loadingView;
    protected ViewGroup noNetView;
    protected ViewGroup noDataView;
    protected Handler handler = new Handler();

    public DefaultTipsView(Context context) {
        this(context, null);
    }

    public DefaultTipsView(Context context, View realView) {
        this(context, realView, true);
    }

    public DefaultTipsView(Context context, View realView, boolean inflateDefault) {
        super(context, realView);
        if (inflateDefault) {
            inflateContentView();
            loadingView = (ViewGroup) findViewById(R.id.loadingView);
            noNetView = (ViewGroup) findViewById(R.id.noNetView);
            noDataView = (ViewGroup) findViewById(R.id.noDataView);
            loadingView.setVisibility(VISIBLE);
            noNetView.setVisibility(GONE);
            noDataView.setVisibility(GONE);
        }
    }

    protected void inflateContentView() {
        View view = LayoutInflater.from(context).inflate(R.layout.view_tips, this, true);
    }

    public void setLoadingView(int resID) {
        if (loadingView != null) {
            loadingView.removeAllViews();
            LayoutInflater.from(context).inflate(resID, loadingView, true);
        }
    }

    public void setLoadingView(View view) {
        if (loadingView != null) {
            loadingView.removeAllViews();
            loadingView.addView(view);
        }
    }

    public void setNoNetworkView(int resID) {
        if (noNetView != null) {
            noNetView.removeAllViews();
            LayoutInflater.from(context).inflate(resID, noNetView, true);
        }
    }

    public void setNoNetworkView(View view) {
        if (noNetView != null) {
            noNetView.removeAllViews();
            noNetView.addView(view);
        }
    }

    public void setNoDataView(int resID) {
        if (noDataView != null) {
            noDataView.removeAllViews();
            LayoutInflater.from(context).inflate(resID, noDataView, true);
        }
    }

    public void setNoDataView(View view) {
        if (noDataView != null) {
            noDataView.removeAllViews();
            noDataView.addView(view);
        }
    }

    public ViewGroup getNoDataViewGroup() {
        return noDataView;
    }

    public void showLoading() {
        showSpecifyView(loadingView);
    }

    public void showNoNetwork() {
        showSpecifyView(noNetView);
    }

    public void showNoData() {
        showSpecifyView(noDataView);
    }

    public void showSpecifyView(View view) {
        showTipsView();
        hideAllChildView();
        if (view != null) {
            view.setVisibility(VISIBLE);
        }
    }

    public void hideAllChildView() {
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).setVisibility(GONE);
        }
    }
}
